home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue79 / misc / tougher / request.cpp.txt < prev    next >
Encoding:
Text File  |  2002-08-14  |  3.6 KB  |  160 lines

  1. //
  2. //
  3. // Copyright 2002 Rob Tougher <robt@robtougher.com>
  4. //
  5. // This file is part of xmlrpc.
  6. //
  7. // xmlrpc is free software; you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation; either version 2 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // xmlrpc is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with xmlrpc; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20. //
  21. //
  22.  
  23.  
  24. // Implementation of the request class
  25.  
  26. #include "request.hpp"
  27. #include "xmlrpc_exceptions.hpp"
  28.  
  29. namespace xmlrpc
  30. {
  31.   request::request ( const std::string name ) : m_name ( name ) {}
  32.  
  33.   request::~request() {}
  34.  
  35.   //
  36.   // <request>
  37.   //   <name></name>
  38.   //   <params>
  39.   //      <param>
  40.   //         <name></name><value></value>
  41.   //      </param>
  42.   //   </params>
  43.   // </request>
  44.   //
  45.   std::string request::get_xml() const
  46.   {
  47.     xml::node root ("request" );
  48.     xml::node * request_name = root.add_child ( "name", m_name );
  49.     xml::node * params = root.add_child ( "params" );
  50.  
  51.     for ( std::vector<param>::const_iterator it = m_params.begin();
  52.       it != m_params.end();
  53.       it++ )
  54.       {
  55.     param p = (*it);
  56.     xml::node * param_node = params->add_child ( "param" );
  57.  
  58.     xml::node * param_name = param_node->add_child ( "name",
  59.                              p.get_name() );
  60.  
  61.     xml::node * param_value = param_node->add_child ( "value",
  62.                               p.get_value() );
  63.       }
  64.  
  65.     return root.get_xml();
  66.   }
  67.  
  68.  
  69.   //
  70.   // <request>
  71.   //   <name></name>
  72.   //   <params>
  73.   //      <param>
  74.   //         <name></name><value></value>
  75.   //      </param>
  76.   //   </params>
  77.   // </request>
  78.   //
  79.   void request::load_xml ( const std::string s )
  80.   {
  81.     m_params.clear();
  82.     m_name = "";
  83.  
  84.     xml::node n;
  85.  
  86.     try
  87.       {
  88.     n.load_xml ( s );
  89.       }
  90.     catch ( xml::parse_exception& e )
  91.       {
  92.     throw request_exception ( "Could not parse the request." );
  93.       }
  94.  
  95.     if ( n.get_child_count() > 0 )
  96.       {
  97.     xml::node * request = n.get_child ( 0 );
  98.  
  99.     if ( request && request->get_name().compare ( "name" ) == 0 )
  100.       {
  101.         m_name = request->get_text();
  102.       }
  103.     else
  104.       {
  105.         throw request_exception
  106.           ( "<name> is not the first child node of the request." );
  107.       }
  108.  
  109.     xml::node * params = n.get_child ( 1 );
  110.  
  111.     if ( params )
  112.       {
  113.         int param_count = params->get_child_count();
  114.  
  115.         for ( int param_index = 0; param_index < param_count; param_index++ )
  116.           {
  117.         xml::node * p = params->get_child ( param_index );
  118.  
  119.         if ( p && p->get_name().compare ( "param" ) == 0 &&
  120.              p->get_child_count() == 2 )
  121.           {
  122.             xml::node *name = p->get_child ( 0 );
  123.             xml::node *value = p->get_child ( 1 );
  124.  
  125.             if ( name && name->get_name() == "name" &&
  126.              value && value->get_name() == "value" )
  127.               {
  128.             m_params.push_back ( param ( name->get_text(), 
  129.                              value->get_text() ) );
  130.               }
  131.             else
  132.               {
  133.             throw request_exception ( "Param not properly formed." );
  134.               }
  135.           }
  136.           }
  137.       }
  138.     else
  139.       {
  140.         throw request_exception ( "Request does not contain a params collection." );
  141.       }
  142.       }
  143.   }
  144.  
  145.  
  146.  
  147.   param request::get_param ( const std::string name ) const
  148.   {
  149.     for ( std::vector<param>::const_iterator it = m_params.begin();
  150.       it != m_params.end();
  151.       it++ )
  152.       {
  153.     param p = *(it);
  154.     if ( p.get_name() == name ) return p;
  155.       }
  156.  
  157.     throw request_exception ( "Param with specified name not found." );
  158.   }
  159. };
  160.